<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="姓名" prop="employeeName">
        <el-input
          v-model="queryParams.employeeName"
          placeholder="请输入姓名"
          clearable
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考勤日期" prop="attendanceDate">
        <el-date-picker
          v-model="queryParams.attendanceDate"
          type="date"
          placeholder="选择考勤日期"
          clearable
          style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="考勤状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择考勤状态" clearable style="width: 200px">
          <el-option label="正常" value="normal" />
          <el-option label="迟到" value="late" />
          <el-option label="早退" value="early" />
          <el-option label="缺勤" value="absent" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['attendance:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['attendance:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['attendance:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['attendance:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="attendanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="employeeName" />
      <el-table-column label="考勤日期" align="center" prop="attendanceDate" />
      <el-table-column label="考勤状态" align="center" prop="status">
        <template #default="scope">
          {{ statusMap[String(scope.row.status)] || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="考勤时间(小时)" align="center" prop="hoursWorked" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="text" icon="Edit" @click="handleUpdate(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改考勤对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="attendanceRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工姓名" prop="employeeName">
          <el-input v-model="form.employeeName" placeholder="请输入员工姓名" />
        </el-form-item>
        <el-form-item label="考勤日期" prop="attendanceDate">
          <el-date-picker
            v-model="form.attendanceDate"
            type="date"
            placeholder="选择考勤日期"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="签到时间" prop="checkInTime">
          <el-time-picker
            v-model="form.checkInTime"
            placeholder="选择签到时间"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="签退时间" prop="checkOutTime">
          <el-time-picker
            v-model="form.checkOutTime"
            placeholder="选择签退时间"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="考勤状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择考勤状态">
            <el-option label="正常" value="normal" />
            <el-option label="迟到" value="late" />
            <el-option label="早退" value="early" />
            <el-option label="缺勤" value="absent" />
          </el-select>
        </el-form-item>
        <el-form-item label="考勤时间" prop="hoursWorked">
          <el-input-number 
            v-model="form.hoursWorked" 
            :precision="1" 
            :step="0.5" 
            :min="0" 
            :max="24"
            placeholder="请输入考勤时间"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { listAttendance, getAttendance, delAttendance, addAttendance, updateAttendance } from '@/api/system/attendance'

const { proxy } = getCurrentInstance();

// 遮罩层
const loading = ref(true);
// 选中数组
const ids = ref([]);
// 非单个禁用
const single = ref(true);
// 非多个禁用
const multiple = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
// 考勤表格数据
const attendanceList = ref([]);
// 弹出层标题
const title = ref("");
// 是否显示弹出层
const open = ref(false);

// 考勤状态数据字典
const statusOptions = ref([
  { dictValue: "normal", dictLabel: "正常" },
  { dictValue: "late", dictLabel: "迟到" },
  { dictValue: "early", dictLabel: "早退" },
  { dictValue: "absent", dictLabel: "缺勤" }
]);

// 查询参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  employeeName: undefined,
  attendanceDate: undefined,
  status: undefined
});

// 表单参数
const form = ref({});
// 表单校验
const rules = ref({
  employeeName: [
    { required: true, message: "员工姓名不能为空", trigger: "blur" }
  ],
  attendanceDate: [
    { required: true, message: "考勤日期不能为空", trigger: "blur" }
  ],
  checkInTime: [
    { required: true, message: "签到时间不能为空", trigger: "blur" }
  ],
  checkOutTime: [
    { required: true, message: "签退时间不能为空", trigger: "blur" }
  ],
  status: [
    { required: true, message: "考勤状态不能为空", trigger: "change" }
  ],
  hoursWorked: [
    { required: true, message: "考勤时间不能为空", trigger: "blur" }
  ]
});

const statusMap = {
  '0': '出勤',
  '1': '休假',
  '2': '事假',
  '3': '矿工'
}

/** 查询考勤列表 */
function getList() {
  loading.value = true;
  listAttendance(queryParams.value).then(response => {
    attendanceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    id: undefined,
    employeeName: undefined,
    attendanceDate: undefined,
    checkInTime: undefined,
    checkOutTime: undefined,
    status: undefined,
    hoursWorked: undefined,
    remark: undefined
  };
  proxy.resetForm("attendanceRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryForm");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加考勤";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value[0];
  getAttendance(id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改考勤";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["attendanceRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        updateAttendance(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addAttendance(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const attendanceIds = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除考勤编号为"' + attendanceIds + '"的数据项？').then(function() {
    return delAttendance(attendanceIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('attendance/export', {
    ...queryParams.value
  }, `attendance_${new Date().getTime()}.xlsx`);
}

getList();
</script> 